package com.gxa.controller.municipalsys;

import com.gxa.common.R;
import com.gxa.service.CitySysUserService;
import io.swagger.annotations.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;


@Controller
@Api(value = "市级用户登录",tags = "市级用户登录接口API")
@RequestMapping("/city_sys")
public class CityLoginController {

    @Autowired
    private CitySysUserService citySysUserService;

    /**
     * 市级用户登录接口
     * @param userName
     * @param password
     * @param response
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "市级用户登录接口",notes = "验证账户和密码",httpMethod = "POST", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "账户",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "password",value = "密码",paramType = "query",dataType = "String",required = true)
    })
    @ApiResponses({
            @ApiResponse(code=200,message="登录成功"),
            @ApiResponse(code=401,message="token为空"),
            @ApiResponse(code=1005,message="Token过期"),
            @ApiResponse(code=1006,message="Token验证不通过"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public R login(String userName,String password, HttpServletResponse response){

        return citySysUserService.login(userName,password,response);
    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    @ApiOperation(value = "系统用户退出接口",notes = "系统用户退出",httpMethod = "GET", response = R.class)
    @ResponseBody
    public R logout(){
        R r = new R();
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()) {
            subject.logout();
            r.setCode(200);
            r.setMsg("退出成功");
        }

        return r;
    }
}
